package com.jgp.security.controller;

import com.jgp.common.annotation.UIParam;
import com.jgp.common.pojo.TreeBean;
import com.jgp.security.annotation.Power;
import com.jgp.security.config.SecUserAdapter;
import com.jgp.security.pojo.UserInfo;
import com.jgp.security.secmodel.SecInstitution;
import com.jgp.security.service.SecInstitutionService;
import com.jgp.sys.annotation.PowerGroup;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-03
 */
@Log4j2
@PowerGroup("机构管理")
@RestController
@RequestMapping("/security/institution-api-ctl")
public class InstitutionApiController extends JGPBackController {
    
    @Autowired
    private SecInstitutionService service;
    
    @Autowired
    private SecUserAdapter userAdapter;
    
    @Power(remark = "保存")
    @RequestMapping("save")
    public Result createInstitution(SecInstitution institution) {
        service.save(institution);
        return ajaxRe(true);
    }
    
    @RequestMapping("query-tree")
    public List<TreeBean> queryTree() {
        UserInfo userInfo = userAdapter.currentUser();
        String clientId = userInfo.getClientId();
        if(StringUtils.isBlank(clientId)){
            clientId = "all";
        }
        return service.queryFullTreeFromCache(clientId);
    }
    
    @RequestMapping("query-tree-lazy")
    public List<TreeBean> queryTreeLazy(Long parentId) {
        return service.queryTreeLazy(parentId);
    }
    
    @RequestMapping("query-one")
    public Result queryOne(Long id) {
        SecInstitution institution = service.queryOne(id);
        return ajaxReData("institution", institution);
    }
    
    @RequestMapping("query")
    public Result query(Long parentId, String name, @UIParam("pager") Pager pager) {
        List list = service.query(parentId, name, pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("图标", "icon"));
        categories.add(new GridResult.Category("角色名", "name"));
        categories.add(new GridResult.Category("备注", "remark"));
        return ajaxReGrid("gdata", categories, list, pager);
    }
    
    @RequestMapping("remove-institution")
    public Result removeInstitution(@RequestBody List<Long> ids) {
        service.removeInstitution(ids);
        return ajaxRe(true);
    }
    
    @RequestMapping("refresh-cache")
    public Result refreshCache() {
        UserInfo userInfo = userAdapter.currentUser();
        String clientId = userInfo.getClientId();
        if(StringUtils.isBlank(clientId)){
            clientId = "all";
        }
        service.removeCache(clientId);
        service.queryFullTreeFromCache(clientId);
        return ajaxRe(true);
    }
    
}
